package com.atstudy.servlet;

import com.atstudy.entites.Account;
import com.atstudy.service.AccountServiceImpl;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @version v1.0
 * @description:
 * @author: admin
 * @date: 2023/1/30 16:18
 * @copyright: 博为峰
 */
@WebServlet(name = "s01", urlPatterns = {"/s01"})
public class AccountServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id");
        AccountServiceImpl accountService = new AccountServiceImpl();
        Account account = accountService.getAccountById(Integer.parseInt(id));
//        System.out.println("account = " + account);
//        PrintWriter pw = resp.getWriter();
//        pw.write("<h3>" + account.getName() + "</h3>");


        //ServletContext域中
//        ServletContext servletContext = this.getServletContext();
//        servletContext.setAttribute("account", account);

        //HttpServletRequest
        req.setAttribute("account", account);

        //转发
        RequestDispatcher dispatcher = req.getRequestDispatcher("/el.jsp");
        dispatcher.forward(req, resp);
    }

    public void add() {
        System.out.println("AccountServlet add");
    }

    public void del() {
        System.out.println("AccountServlet del");
    }
}
